package com.zhouyibike.uu.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by gyy on 2015/12/31.
 * 时间格式化的类
 */
public class LYDate extends Date {

    private static DateFormat dateTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getDateTimeString(Date date) {
        if (date == null) return "";
        return dateTimeF.format(date);
    }

    public static Date getDateFromDateTimeString(String dateTimeString) {
        try {
            return dateTimeF.parse(dateTimeString);
        } catch (ParseException e) {
            Log.e("getDateg: ", "ads");
            return null;
        }
    }

    public static DateFormat dateShortTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static String getDateShortTimeString(Date date) {
        if (date == null) return "";
        return dateShortTimeF.format(date);
    }

    public static Date getDateFromDateShortTimeString(String dateShortTimeString) {
        try {
            return dateShortTimeF.parse(dateShortTimeString);
        } catch (ParseException e) {
            return null;
        }
    }

    public static DateFormat dateShortTimeY = new SimpleDateFormat("yyyy");

    public static String getDateShortTimeyearString(Date date) {
        if (date == null) return "";
        return dateShortTimeY.format(date);
    }

    public static Date getDateFromDateShortTimeyearString(String dateShortTimeString) {
        try {
            return dateShortTimeY.parse(dateShortTimeString);
        } catch (ParseException e) {
            return null;
        }
    }

    public static DateFormat dateE = new SimpleDateFormat("yyyy-MM");

    public static String getYearAndMonthString(Date date) {
        if (date == null) return "";
        return dateE.format(date);
    }

    public static DateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");

    public static String getDateString(Date date) {
        if (date == null) return "";
        return dateF.format(date);
    }

    public static Date getDateFromDateString(String dateString) {
        try {
            return dateF.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }

    private static DateFormat shortDateTimeF = new SimpleDateFormat("MM-dd HH:mm");

    public static String getShortDateTimeString(Date date) {
        if (date == null) return "";
        return shortDateTimeF.format(date);
    }

    private static DateFormat shortDateF = new SimpleDateFormat("MM-dd");

    public static String getShortDateString(Date date) {
        if (date == null) return "";
        return shortDateF.format(date);
    }

    public static DateFormat shortTimeF = new SimpleDateFormat("HH:mm");

    public static String getShortTimeString(Date date) {
        if (date == null) return "";
        return shortTimeF.format(date);
    }

    private static Calendar calendar = Calendar.getInstance();

    public static Date addDayFromDate(Date date, int day) {
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        return calendar.getTime();
    }


    //返回大写日期
    public static String getUpperDateTimeString(Date date) {

        try {
            calendar.setTime(date);

            return LYDate.toUpperNumber(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))) + "时"
                    + LYDate.toUpperNumber(String.valueOf(calendar.get(Calendar.MINUTE))) + "分";
        } catch (Exception e) {
            e.printStackTrace();
            //// TODO: 2017/4/25 修改时间 
            return "";
        }
    }


    public static String toUpperNumber(String dateString) {
        if (dateString==null||dateString.equals("")) {
            return "";
        }
        if (dateString.length() == 1) {
            dateString = "0" + dateString;
        }

        String string = null;
        String firNumStr = dateString.substring(0, 1);
        String secNumStr = dateString.substring(1, 2);
        int firNum = Integer.valueOf(firNumStr);
        int secNum = Integer.valueOf(secNumStr);
        String[] numArray = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
        if (firNum == 0) {
            string = numArray[secNum];
        } else if (firNum == 1) {
            if (secNum == 0) {
                string = numArray[10];
            } else {
                string = numArray[10] + numArray[secNum];
            }
        } else {
            if (secNum == 0) {
                string = numArray[firNum] + numArray[10];
            } else {
                string = numArray[firNum] + numArray[10] + numArray[secNum];
            }
        }
        return string;
    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static String getFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = dateF.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
        return str.toString();

    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static String getLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        Date theDate = calendar.getTime();
        String s = dateF.format(theDate);
        StringBuffer str = new StringBuffer().append(s).append(" 23:59:59");
        return str.toString();

    }


    public static Date addMinute(String time, int minute) {
        Date date = getDateFromDateTimeString(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, (0 - minute));
        Date theDate = calendar.getTime();
        return theDate;
    }


    /*
   * 计算两个日期相差的天数
   */
    public static int daysOfTwo(Date fDate, Date oDate) {
        long day1 = fDate.getTime();
        long day2 = oDate.getTime();
        long time = day2 - day1;
        int day = (int) time / 1000 / 60 / 60 / 24;
        return day;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)      //bu同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) //闰年
                {
                    timeDistance += 366;
                } else        //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else {           //同年
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    //加天数
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        Date theDate = calendar.getTime();
        return theDate;
    }

    //减天数
    public static Date decDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, (0 - day));
        Date theDate = calendar.getTime();
        return theDate;
    }

    //加月数
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        Date theDate = calendar.getTime();
        return theDate;
    }

    //减月数
    public static Date decMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, (0 - month));
        Date theDate = calendar.getTime();
        return theDate;
    }

    //加年数
    public static Date addYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);
        Date theDate = calendar.getTime();
        return theDate;
    }

    //减年数
    public static Date decYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, (0 - year));
        Date theDate = calendar.getTime();
        return theDate;
    }
    public static String makemonthtrue(int month) {
        String str="";
        if (month<10)
        {
            str="0"+month;
        }else
        {
            str=""+month;
        }
        return str;
    }
    //判断是周几
    public static String Weeknum(String time) {
        String Week = "星期";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//也可将此值当参数传进来
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch(c.get(Calendar.DAY_OF_WEEK)){
            case 1:
                Week += "日";
                break;
            case 2:
                Week += "一";
                break;
            case 3:
                Week += "二";
                break;
            case 4:
                Week += "三";
                break;
            case 5:
                Week += "四";
                break;
            case 6:
                Week += "五";
                break;
            case 7:
                Week += "六";
                break;
            default:
                break;
        }
        return Week;
    }
    public static int Weeknum2(String time) {
        String Week = "星期";
        int weeknum=0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//也可将此值当参数传进来
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch(c.get(Calendar.DAY_OF_WEEK)){
            case 1:
                Week += "日";
                weeknum=7;
                break;
            case 2:
                Week += "一";
                weeknum=1;
                break;
            case 3:
                Week += "二";
                weeknum=2;
                break;
            case 4:
                Week += "三";
                weeknum=3;
                break;
            case 5:
                weeknum=4;
                Week += "四";
                break;
            case 6:
                weeknum=5;
                Week += "五";
                break;
            case 7:
                weeknum=6;
                Week += "六";
                break;
            default:
                break;
        }
        return weeknum;
    }
}
